home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / Apps / SoundApps / aa_m68k_Only / Sonogram / convs2m.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-20  |  3.4 KB  |  119 lines

  1.  
  2. //
  3. //    convs2m        A stereo to monaural sound converter
  4. //
  5. //    1991/3/18
  6. //    HiroshiMomose
  7. //    Zoology, UCD, Davis, CA 95616   hmomose@ucdavis.edu
  8. //
  9. //    To compile this program, type : cc -O -g -o convs2m convs2m.c -lsys_s
  10. //
  11.  
  12. #define title ("convs2m.c - converts a stereo 16 bit linear .snd file to monaural")
  13.  
  14. #import <sound/sound.h>
  15. #import <stdio.h>
  16.  
  17. #define MONO    1
  18. #define STEREO    2
  19.  
  20. check_error(int err)
  21. {
  22.     if (err) {
  23.     fprintf(stderr, "Error : %s\n",SNDSoundError(err));
  24.     exit(1);
  25.     }
  26.     return err;
  27. }
  28.  
  29. main (int argc, char *argv[])
  30. {
  31.  
  32.     int        i, size, width, temp, nsamples;
  33.     int        datasize, dataformat, samplingrate, channelcount, info;
  34.     char        channel = 'L';
  35.     short int    ldata, rdata;
  36.     char        *p;
  37.     short int    *ip;
  38.     SNDSoundStruct    *infile;    // for input, we use NeXT SND routines
  39.     FILE        *outfile;    // for output, we don't.
  40.  
  41.  
  42.     // ========== Check command line flag
  43.     if ( argc == 4 )
  44.         channel = toupper( argv[1][0] );
  45.     if ( ( channel != 'L') && ( channel != 'R' ) ) {
  46.         fprintf( stderr, "Error(convs2m) : wrong channel %c (must be L or R)\n", channel );
  47.         exit( -1 );
  48.     }    
  49.     if( argc < 4 ) {
  50.         fprintf( stderr, "%s\n\n", title );
  51.         fprintf( stderr, "Usage : convs2m channel infile outfile\n\t" );
  52.         fprintf( stderr, "channel must be either L or R (case does not matter)\n\t" );
  53.         fprintf( stderr, "infile must be a 16bit linear stereo .snd file\n\t" );
  54.         fprintf( stderr, "outfile will be a 16bit linear monaural .snd file\n\t" );
  55.         fprintf( stderr, "Doesn't check if outfile already exists. So be careful.\n\n" );
  56.         fprintf( stderr, "Sample operation : convs2m L stereo.snd mono.snd\n" );
  57.         exit( -1 );
  58.     }    
  59.  
  60.     // ========== Open input file
  61.     check_error( SNDReadSoundfile( argv[2], &infile ));
  62.     check_error( SNDGetDataPointer( infile, &p, &size, &width ));
  63.                             // probablly width is 2(bytes=16bit)
  64.     ip = (short int *)p;
  65.  
  66.     // ========== Read input file header
  67.     dataformat    = infile->dataFormat;
  68.     datasize    = infile->dataSize;
  69.     samplingrate    = infile->samplingRate;
  70.     channelcount    = infile->channelCount;
  71.     nsamples = datasize / channelcount / width;    // no. of data samples
  72.  
  73.     // ========== Check file format
  74.     if( channelcount != STEREO ) {
  75.         fprintf( stderr, "Error(convs2m) : Input file %s is not stereo\n", argv[1] );
  76.         exit( -1 );
  77.     }    
  78.     if( dataformat != SND_FORMAT_LINEAR_16 ) {
  79.         fprintf( stderr, "Error(convs2m) : Input file %s is not 16bit linear\n", argv[1] );
  80.         exit( -1 );
  81.     }    
  82.  
  83.     // ========== open output file
  84.     if (( outfile = fopen ( argv[3], "wb" )) == 0 ) {
  85.         fprintf( stderr, "Error(convs2m) : Can't open input file : %s\n", argv[1] );
  86.         exit( -1 );
  87.     }
  88.  
  89.     datasize /= 2;        // Size of the data will be half
  90.  
  91.     // ========== Write output file header    
  92.     temp = SND_MAGIC;    fwrite( &temp, 4, 1, outfile );
  93.     temp = 28;        fwrite( &temp, 4, 1, outfile );    // start point of sound data
  94.     temp = datasize;    fwrite( &temp, 4, 1, outfile );    // no. of bytes in sound data
  95.     temp = dataformat;    fwrite( &temp, 4, 1, outfile );
  96.     temp = samplingrate;    fwrite( &temp, 4, 1, outfile );
  97.     temp = MONO;        fwrite( &temp, 4, 1, outfile );    // channel count
  98.     temp = 0x00000000;      fwrite( &temp, 4, 1, outfile );    // info. string
  99.  
  100.     // ========== Write output sound data    
  101.     if ( channel == 'L' ) {
  102.         for ( i = 0; i < nsamples; i++ ) {
  103.             ldata = ( *(ip++) );
  104.             rdata = ( *(ip++) );
  105.             fwrite ( &ldata, 2, 1, outfile );
  106.         }
  107.     } else {
  108.         for ( i = 0; i < nsamples; i++ ) {
  109.             ldata = ( *(ip++) );
  110.             rdata = ( *(ip++) );
  111.             fwrite ( &rdata, 2, 1, outfile );
  112.         }
  113.     }
  114.     fclose( outfile );
  115.     exit( 0 );
  116. }
  117.  
  118.  
  119.